package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.mall.lib.domain.MessageReplyRuleContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by caihongli on 2017/6/7.
 */
@Repository
public class MallWechatMessageReplyRuleContentRepositoryImpl implements MallWechatMessageReplyRuleContentRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MallWechatMessageReplyRuleContentRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 通过规则ID随机查找一条内容信息
     * @param ruleId
     * @return
     */
    @Override
    public MessageReplyRuleContent findOneMessageReplyContentByruleId(Long ruleId) {
        SQLObjectDoing<MessageReplyRuleContent> doing = (sql, fields, params, map) -> {
            sql.append("select %s from message_reply_rule_content m where m.message_reply_rule_id = :ruleId order by rand() limit 1");
            params.put("ruleId", ruleId);
            fields.append("m.*");
        };
        return doing.go(MessageReplyRuleContent.class, jdbcTemplate, log, objectMapper);
    }
}
